import {
  request
} from "../../request/index";

// pages/list/list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabsList: ['综合', '销量', '价格'],
    goodsList:[],
    renderList:[],
    pagenum:1,
    total:0,
    TabCur: 0,
    scrollLeft: 0
  },
  tabSelect(e) {
    const text = e._relatedInfo.anchorTargetText
    let type = ''
    let renderList = [];
    switch(text){
      case "综合":
        renderList = this.__data__.goodsList;
        break;
      case "销量":
        renderList = this.__data__.goodsList;
        break;
      case "价格":
        type="goods_price"
        renderList = JSON.parse(JSON.stringify(this.__data__.goodsList)).sort((a,b)=>a[type] - b[type])
    }
    
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60,
      renderList,
    })
  },
  async goodsSearch(cid) {
    const data = await request({
      url: "/goods/search",
      data: {
        cid
      }
    })
    const goodsList =  data.goods.filter(item=>item.goods_small_logo)
    this.setData({
      goodsList:goodsList,
      renderList:goodsList,
      total:data.total,
      pagenum: data.pagenum,
    })
    console.log(data);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    const {
      cid
    } = options;
    this.goodsSearch(cid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})